<template>
  <div class="login-container">
    <div class="header-container">
      <img class="logo-img" src="../../assets/login/logo.png" />
      <div class="title">重庆市国土整治项目管理系统</div>
    </div>
    <div class="content-container">
      <div class="left">
        <div class="title">一致性、完整性和安全性综合监管平台</div>
        <div class="desc">实现对内市国土整治中心内部系统互联，对外能与自然资源部，重庆农村土地交易所系统互通，内外网系统间数据交换，形成大系统融合的格局。进一步提升重庆市农村土地整治中心信息化管理水平，提高土地整治工作效率。</div>
      </div>
      <div class="login-box">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
          <div class="title-container">
            <div class="title">
              欢迎登录到
              <br />国土整治管理系统
            </div>
          </div>

          <el-form-item prop="username">
            <el-input ref="username" v-model="loginForm.username" placeholder="请输入账号" name="username" type="text" tabindex="1" auto-complete="on" />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <el-button :loading="loading" type="primary" style="width:100%;height:0.46rem;font-size: 0.16rem;line-height: 0.46rem;padding: 0;display: block;" @click.native.prevent="handleLogin">登录</el-button>
        </el-form>
        <div class="foot-box">————&nbsp;&nbsp;&nbsp;&nbsp;重庆市农村土地整治中心&nbsp;&nbsp;&nbsp;&nbsp;————</div>
      </div>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";

export default {
  name: "Login",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        username: "user9",
        password: "123456"
        // username: "",
        // password: ""
      },
      loginRules: {
        username: [
          // { required: true, trigger: "blur", validator: validateUsername }
          { required: true, trigger: "blur", message: "请输入账号" }
        ],
        password: [
          // { required: true, trigger: "blur", validator: validatePassword }
          { required: true, trigger: "blur", message: "请输入密码" }
        ]
      },
      loading: false,
      passwordType: "password",
      redirect: undefined
    };
  },
  created() {
    const that = this;
    (function(doc, win) {
      var docEl = doc.documentElement,
        resizeEvt = "orientationchange" in window ? "orientationchange" : "resize",
        recalc = function() {
          if (that.$route.path == "/login") {
            var clientWidth = docEl.clientWidth;
            if (!clientWidth) return;
            if (clientWidth >= 1920) {
              docEl.style.fontSize = "100px";
            } else {
              docEl.style.fontSize = 100 * (clientWidth / 1920) + "px";
            }
          }
        };
      if (!doc.addEventListener) return;
      win.addEventListener(resizeEvt, recalc, false);
      doc.addEventListener("DOMContentLoaded", recalc, false);
      recalc();
    })(document, window);
  },
  destroyed() {
    (function(doc, win) {
      var docEl = doc.documentElement;
      docEl.style.fontSize = "";
    })(document, window);
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || "/" });
              this.loading = false;
            })
            .catch(err => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #999;
$cursor: #999;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container /deep/ .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  position: relative;

  .header-container {
    position: absolute;
    top: 0.62rem;
    left: 0.6rem;
    display: flex;
    flex-direction: row;
    align-items: center;

    .logo-img {
      display: block;
      width: 0.52rem;
      height: 0.52rem;
    }

    .title {
      margin-left: 0.12rem;
      font-size: 0.36rem;
      font-family: PingFang SC;
      font-weight: 500;
      color: rgba(255, 255, 255, 1);
    }
  }

  /deep/ .el-input {
    display: inline-block;
    height: 0.48rem;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 0.12rem 0.05rem 0.12rem 0.15rem;
      color: $light_gray;
      height: 0.48rem;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0 10rem $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  /deep/ .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: #fff;
    border-radius: 0.05rem;
    color: #454545;
  }

  .content-container {
    width: 70%;
    height: 6.4rem;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    justify-content: space-between;
    align-items: center;

    .left {
      width: 4.58rem;
      text-align: center;

      .title {
        width: 4.4rem;
        margin: 0 auto;
        font-size: 0.38rem;
        font-family: PingFang SC;
        font-weight: 500;
        line-height: 1.5;
        color: rgba(255, 255, 255, 1);
        margin-bottom: 0.2rem;
      }

      .desc {
        font-size: 0.14rem;
        font-family: PingFang SC;
        font-weight: 300;
        line-height: 1.8;
        color: rgba(230, 230, 230, 1);
      }
    }

    .login-box {
      width: 5.6rem;
      height: 6.4rem;
      background: rgba(255, 255, 255, 1);
    }
    .login-box /deep/ .el-form-item__content {
      border: 1px solid rgba(191, 191, 191, 1);
    }
  }
}
</style>


<style>
  .el-message {
    min-width: 3.8rem !important;
  }
  .el-message .el-icon-error {
    font-size: 0.2rem;
  }
</style>

<style lang="scss">
$bg: #08182f;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
  background: url("../../assets/login/login_bg.png") no-repeat;
  background-size: 100% 100%;
  overflow: hidden;

  .login-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }

  .logo-box {
    background: url(../../assets/login/logo.png) no-repeat;
    width: 1.2rem;
    height: 1.2rem;
    display: block;
    margin: 0 auto;
  }
  .title-box {
    font-size: 0.4rem;
    color: #fff;
    text-align: center;
    margin-top: 0.5rem;
  }
  .foot-box {
    width: 100%;
    text-align: center;
    font-size: 0.12rem;
    color: #ccc;
    margin-top: 0.88rem;
    font-family: PingFang SC;
    font-weight: 500;
    color: rgba(153, 153, 153, 1);
  }
  .login-form {
    position: relative;
    width: 4.56rem;
    max-width: 100%;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 0.14rem;
    color: #fff;
    margin-bottom: 0.1rem;

    span {
      &:first-of-type {
        margin-right: 0.16rem;
      }
    }
  }

  .svg-container {
    padding: 0.06rem 0.05rem 0.06rem 0.15rem;
    color: $dark_gray;
    vertical-align: middle;
    width: 0.3rem;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 0.36rem;
      font-family: PingFang SC;
      font-weight: bold;
      line-height: 1.5;
      color: rgba(51, 51, 51, 1);
      text-align: center;
      padding: 0.88rem 0.48rem 0.64rem 0.48rem;
    }
  }

  .show-pwd {
    position: absolute;
    right: 0.1rem;
    top: 0.08rem;
    font-size: 0.16rem;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
